#ifndef BORDERPROGRESSLABEL_H
#define BORDERPROGRESSLABEL_H
#include <QLabel>
#include <QPainter>
#include <QPainterPath>
class BorderLabel : public QLabel
{
public:
	explicit BorderLabel(QWidget *parent = nullptr) : QLabel(parent) {}
	QSize sizeHint() const override
	{
		QFontMetrics fm(font());
		QStringList parts = text().split("**");
		int totalWidth = 0;
		for (const QString &part : parts) totalWidth += fm.horizontalAdvance(part);
		return QSize(totalWidth + 2, fm.height() + 3);
	}
protected:
	void paintEvent(QPaintEvent *event) override
	{
		QPainter painter(this);
		painter.setRenderHint(QPainter::Antialiasing, false);
		painter.setFont(this->font());
		QStringList parts = text().split("**");
		int x = 1;
		for (int i = 0; i < parts.size(); ++i) {
			if (parts[i].isEmpty()) continue;
			QFontMetrics fm(this->font());
			QPainterPath path;
			path.addText(x, this->height() - 6, this->font(), parts[i]);
			painter.setPen(QPen(Qt::black, 6, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
			painter.setBrush(Qt::NoBrush);
			painter.drawPath(path);
			painter.setPen(Qt::NoPen);
			if (i % 2 == 1) painter.setBrush(Qt::red);
			else painter.setBrush(Qt::white);
			painter.drawPath(path);
			x += fm.horizontalAdvance(parts[i]);
		}
	}
};
class BorderProgressLabel : public QLabel
{
	Q_OBJECT
public:
	explicit BorderProgressLabel(QWidget *parent = nullptr) : QLabel(parent), m_progressColor(Qt::blue), m_progress(0.0), m_borderWidth(10) { setAutoFillBackground(false); }
	QColor borderProgressColor() const { return m_progressColor; }
	int borderWidth() const { return m_borderWidth; }
	double progress() const { return m_progress; }
	void setBorderColor(const QColor &color)
	{
		if (m_progressColor != color) {
			m_progressColor = color;
			update();
		}
	}
	void setProgress(double value)
	{
		double clampedValue = qBound(0.0, value, 1.0);
		if (m_progress != clampedValue) {
			m_progress = clampedValue;
			update();
		}
	}
	void setBorderWidth(int width)
	{
		if (width >= 0 && m_borderWidth != width) {
			m_borderWidth = width;
			update();
		}
	}
protected:
	void paintEvent(QPaintEvent *event) override
	{
		QPainter painter(this);
		painter.setRenderHint(QPainter::Antialiasing, false);
		const QRect borderRect = rect();
		const int bw = m_borderWidth;
		int progressLength = qFloor((1.0 - m_progress) * (2 * (borderRect.width() + borderRect.height() - 2 * bw)));
		if (progressLength <= 0) {
			QLabel::paintEvent(event);
			return;
		}
		painter.setPen(Qt::NoPen);
		painter.setBrush(m_progressColor);
		const int segments[4] = {borderRect.width() - bw, borderRect.height() - bw, borderRect.width() - bw, borderRect.height() - bw};
		const int x1 = borderRect.left();
		const int y1 = borderRect.top();
		const int x2 = borderRect.right() - bw + 1;
		const int y2 = borderRect.bottom() - bw + 1;
		for (int i = 0; i < 4 && progressLength > 0; ++i) {
			int draw = qMin(progressLength, segments[i]);
			if (draw <= 0) continue;
			switch (i) {
			case 0:
				painter.drawRect(x1 + bw, y1, draw, bw);
				break;
			case 1:
				painter.drawRect(x2, y1 + bw, bw, draw);
				break;
			case 2:
				painter.drawRect(x2 - draw, y2, draw, bw);
				break;
			case 3:
				painter.drawRect(x1, y2 - draw, bw, draw);
				break;
			}
			progressLength -= draw;
		}
		QLabel::paintEvent(event);
	}
private:
	QColor m_progressColor;
	double m_progress;
	int m_borderWidth;
};
#endif
